package edu.hubu.commons;

import java.lang.reflect.Field;

/**
 * @author moonlan
 * @date 2020/10/23 下午1:11
 */
public class ObjectAssignment {

    public static void assign(Object src, Object dest) {
        if (!src.getClass().equals(dest.getClass())) {
            throw new IllegalArgumentException("src 和 o2的类型不相同");
        }
        Field[] srcFields = src.getClass().getDeclaredFields();
        Field[] destFields = dest.getClass().getDeclaredFields();

        for (int i = 0; i < srcFields.length; i++) {
            srcFields[i].setAccessible(true);
            destFields[i].setAccessible(true);
            try {
                Object value = srcFields[i].get(src);
                if (!srcFields[i].getName().substring(srcFields[i].getName().length() - 2).equalsIgnoreCase("id") && value != null && !value.toString().equals("")) {
                    destFields[i].set(dest, value);
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException(e);
            }
        }
    }

}
